/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import API.Button;
import API.CheckBox;
import API.Label;
import API.MenuBar;
import API.PopupMenu;
import API.TextBox;
import API.TextType;
import API.Title;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author mrbao
 */
public class LoginForm extends Canvas {

    private Title title;
    private Label lblUsername;
    private TextBox txtUsername;
    private Label lblPassword;
    private TextBox txtPassword;
    private CheckBox chkInvisible;
    private CheckBox chkSaveAccount;
    private Button btnLogin;
    private MenuBar menuBar;
    private PopupMenu popupMenu;
    private int padding = 5;
    private int width;
    private boolean isShowPopupMenu;
    private int select = 1;

    public LoginForm() {
        this.setFullScreenMode(true);
        width = getWidth();
        int x = width / 20;
        int y = 0;
        title = new Title("Login", width);
        y += title.getHeight() + padding;
        lblUsername = new Label("Tai khoan", x, y);
        y += lblUsername.getHeight();
        txtUsername = new TextBox("baokhoai2006", TextType.ANY, x, y, width - width / 10);
        y += txtUsername.getHeight() + padding;
        lblPassword = new Label("Mat khau", x, y);
        y += lblPassword.getHeight();
        txtPassword = new TextBox("1091988", TextType.PASSWORD, x, y, width - width / 10);
        y += txtPassword.getHeight() + padding;
        chkInvisible = new CheckBox("Dang nhap an", x, y);
        y += chkInvisible.getHeight() + padding;
        chkSaveAccount = new CheckBox("Luu tai khoan", x, y);
        y += chkInvisible.getHeight() + padding;
        btnLogin = new Button("Dang nhap", y, width);
        menuBar = new MenuBar("Menu", "Thoat", "", width, getHeight());
        String[] menus = {"Dang nhap", "Dang ky", "Gioi thieu", "Tro giup"};
        popupMenu = new PopupMenu(menus, getHeight() - menuBar.getHeight());
        txtPassword.SetParent(this);
        txtUsername.SetParent(this);
        txtUsername.setIsSelect(true);
    }

    protected void paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, width, getHeight());
        if (isShowPopupMenu) {
            popupMenu.Paint(g);
        } else {
            title.Paint(g);
            lblUsername.Paint(g);
            txtUsername.Paint(g);
            lblPassword.Paint(g);
            txtPassword.Paint(g);
            chkInvisible.Paint(g);
            chkSaveAccount.Paint(g);
            btnLogin.Paint(g);
            menuBar.Paint(g);
        }
    }

    private void SetSelect() {
        txtUsername.setIsSelect(false);
        txtPassword.setIsSelect(false);
        chkInvisible.setIsSelect(false);
        chkSaveAccount.setIsSelect(false);
        btnLogin.setIsSelect(false);
        switch (select) {
            case 1:
                txtUsername.setIsSelect(true);
                break;
            case 2:
                txtPassword.setIsSelect(true);
                break;
            case 3:
                chkInvisible.setIsSelect(true);
                break;
            case 4:
                chkSaveAccount.setIsSelect(true);
                break;
            case 5:
                btnLogin.setIsSelect(true);
                break;
        }
    }

    protected void keyPressed(int keyCode) {
        int gameCode = getGameAction(keyCode);
        switch (gameCode) {
            case DOWN:
                if (select < 5) {
                    select++;
                    SetSelect();
                }
                repaint();
                return;
            case UP:
                if (select > 0) {
                    select--;
                    SetSelect();
                }
                repaint();
                return;
            case FIRE:
                FireKey(gameCode);
                repaint();
                return;
        }
        SetKeyPress(keyCode, gameCode);
        repaint();
    }

    private void FireKey(int gameCode) {
        switch (select) {
            case 3:
                chkInvisible.KeyPress(gameCode);
                break;
            case 4:
                chkSaveAccount.KeyPress(gameCode);
                break;
            case 5:
                Login();
                break;
        }
    }

    private void Login() {
        ChatYahoo.DisplaySplashForm(txtUsername.getText(), txtPassword.getText(), chkInvisible.isIsCheck());
    }

    private void SetKeyPress(int keyCode, int gameCode) {
        switch (select) {
            case 1:
                txtUsername.KeyPress(keyCode, gameCode);
                break;
            case 2:
                txtPassword.KeyPress(keyCode, gameCode);
                break;
        }
    }
}
